package org.guoqiang.datasource.handler.controller;

import org.guoqiang.datasource.handler.repository.PlatformConfigFrontColumn;
import org.guoqiang.datasource.handler.repository.PlatformConfigFrontColumnRepository;
import org.guoqiang.datasource.handler.repository.Player;
import org.guoqiang.datasource.handler.repository.PlayerRepository;
import org.guoqiang.datasource.handler.context.DynamicDataSourceContextHolder;
import org.guoqiang.datasource.handler.util.DataSourceUtil;
import org.guoqiang.datasource.handler.vo.DbInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author PC204
 * @Date 2022/3/29 8:32
 * @Version 1.0
 **/
@RestController
public class HelloController {


    @Autowired
    PlatformConfigFrontColumnRepository platformConfigFrontColumnRepository;

    @GetMapping("/test1")
    public String test() {
        List<PlatformConfigFrontColumn> all = platformConfigFrontColumnRepository.findAll();
        System.out.println(all);
        return "ok";
    }

    @Autowired
    PlayerRepository playerRepository;

    @GetMapping("/test222")
    public String test222() {
        List<Player> all = playerRepository.findAll();
        System.out.println(all);
        return "ok";
    }

//    @GetMapping("/test333")
//    public String test333() {
//
//        DataSourceBuilder<?> root = DataSourceBuilder
//                .create()
//                .driverClassName("com.mysql.cj.jdbc.Driver")
//                .password("bdyh@2020")
//                .username("root")
//                .url("jdbc:mysql://localhost:3306/test1?useSSL=false&useUnicode=true&characterEncoding=utf-8");
//
//        DataSource dataSource = root.build();
//
//        DynamicDataSource.dataSourcesMap.put("dbkey", dataSource);
//        DynamicDataSource.setDataSource("dbkey");
//        List<Player> all = playerRepository.findAll();
//        System.out.println(all);
//
//
//        DynamicDataSource.clear();
//        return "ok";
//    }


    @Autowired
    TransactionTemplate transactionTemplate;

    @GetMapping("/test333")
    public String test333() {

        //数据源key
        String newDsKey = System.currentTimeMillis() + "";
        //添加数据源
        DbInfo dbInfo = new DbInfo();

        dbInfo.setDbName("test1");
        dbInfo.setDriveClassName("com.mysql.cj.jdbc.Driver");
        dbInfo.setIp("localhost");
        dbInfo.setPort("3306");
        dbInfo.setUsername("root");
        dbInfo.setPassword("bdyh@2020");


        DataSourceUtil.addDataSourceToDynamic(newDsKey, dbInfo);

        DynamicDataSourceContextHolder.setContextKey(newDsKey);

        // 切换完了，然后去执行这个sql
        List<Player> all = playerRepository.findAll();
        System.out.println(all);


        transactionTemplate.execute((status) -> {

            try {
                Player player = new Player();
                player.setId("600");
                player.setEn_name("baiguoqiang");
                playerRepository.save(player);
            } catch (Exception e) {
                e.printStackTrace();
                status.setRollbackOnly();
            }
            return null;
        });

//        try {
//            TimeUnit.SECONDS.sleep(200);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        //查询表信息
        DynamicDataSourceContextHolder.removeContextKey();


        return "ok";
    }

}
